﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sockets2;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Net;

namespace Sockets2
{
    public class Servidor
    {
        TcpListener tcpListener;
        NetworkStream networkStream;
        StreamWriter streamWriter;
        StreamReader streamReader;
        Socket[] socketForClient = new Socket[10];
        int cantCon = 0;

        public void Start()
        {
            
            // Creamos un TcpListener y le indicamos que
            //puerto va a poner en escucha.
             tcpListener = new TcpListener(9898);            

            //Iniciamos la esucha
            tcpListener.Start();               
        }
        public bool AceptaConexion()
        {
            if (tcpListener.Pending())
            {
                socketForClient[cantCon] = tcpListener.AcceptSocket();
                cantCon += 1;
                return true;
            }
            return false;
             //return socketForClient.Connected;        
        }
        public string recibir(int nroCliente)
        {

                try
                {
                    //Creamos el networkSream, el Reader y el writer
                    networkStream = new NetworkStream(socketForClient[nroCliente]);
                    streamReader = new StreamReader(networkStream);

                    //Esta es la data a enviar.
                    string theString = "";

                    //Esperamos datos del cliente
                    theString = streamReader.ReadLine();
                    streamReader.Close();
                    networkStream.Close();
                    return theString;

                }
                catch (Exception)
                {
                    streamReader.Close();
                    networkStream.Close();
                    return "";
                }
        }
        public int getCantConect()
        {
            return cantCon;
        }
        public bool nuevoMsj(int nroCliente)
        {
            if (socketForClient[nroCliente].Available > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int getNroConec()
        {
            int count = 0;
            for (int i = 0; i < cantCon; i++)
            {
                if (socketForClient[i].Connected)
                {
                    count += 1;
                } 
            }
            return count;
        }
     
        public void enviarArchivo(int nroCliente)
        {
            networkStream = new NetworkStream(socketForClient[nroCliente]);
            streamWriter = new StreamWriter(networkStream);



            string fileName = "aEnviar.txt";
            byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);

            byte[] fileData = File.ReadAllBytes( fileName);
            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            fileData.CopyTo(clientData, 4 + fileNameByte.Length);


            socketForClient[0].Send(clientData);

        }
        public void enviar(string s, int nroCliente)
        {
            try
            {
                networkStream = new NetworkStream(socketForClient[nroCliente]);
                streamWriter = new StreamWriter(networkStream);
                //Escribimos la data en el stream
                streamWriter.WriteLine(s);
                
                //Ahora le decimos que la mande.
                streamWriter.Flush();

                streamWriter.Close();
                networkStream.Close();
            }
            catch (Exception)
            {
                
                
           }
        }
        public void cerrar()
        {
            try
            {
                if (cantCon >0)
                {
                    for (int i = 0; i < cantCon; i++)
                    {
                        socketForClient[i].Close();
                    }
                }
               
            }
            catch (Exception)
            {
                
                throw;
            }

        }
        public bool solicitudEntrante()
        {
            return tcpListener.Pending();
        }

    }
}
